package cn.com.swu.mianyangapp.ui.message;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import cn.com.swu.mianyangapp.R;
import cn.com.swu.mianyangapp.bean.Text;
import org.json.JSONException;
import org.json.JSONObject;

public class EditTextActivity extends AppCompatActivity {

    private String texturl="http://192.168.43.211:8081/";


    Text text=new Text();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text);
        EditText textTitle=findViewById(R.id.etext_Title);
        EditText recvTele=findViewById(R.id.erecv_tele);
        EditText textContent=findViewById(R.id.editTextTextMultiLine);
        Button saveBtn=findViewById(R.id.save_btn);
        Button sendBtn=findViewById(R.id.send_btn);

        Bundle bundle = this.getIntent().getExtras();

        if(bundle!=null)
        {
            text.setTextTitle(bundle.get("mtextTitle").toString());
            text.setTextContent(bundle.get("mtextContent").toString());
            text.setRecvTele(bundle.get("mrecvTele").toString());
            text.setTextID(Integer.parseInt(bundle.get("mtextID").toString()));

            textTitle.setText(text.getTextTitle());
            recvTele.setText(text.getRecvTele());
            textContent.setText(text.getTextContent());
        }


        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String iptTextTitle=textTitle.getText().toString();
                String iptTextContent=textContent.getText().toString();
                String iptRecTele=recvTele.getText().toString();

                if(text.getTextID()!=null){
                    String textID=text.getTextID().toString();


                    String url=texturl+"updateText"+"?textID="+textID+"&textTitle="+iptTextTitle
                            +"&textContent="+iptTextContent+"&recvTele="+iptRecTele;
                    HttpUtil.sendOkHttpRequest(url,new Callback() {
                        @Override
                        public void onFailure(Request request, IOException e) {
                            System.out.println(e);
                        }
                        @Override
                        public void onResponse(Response response) throws IOException {
                            String responseData = response.body().string();
                            Looper.prepare();
                            Toast.makeText(EditTextActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }
                    });
                }

                else{
                    String url=texturl+"saveText"+"?textTitle="+iptTextTitle
                     +"&textContent="+iptTextContent+"&recvTele="+iptRecTele+"&sendTele="+"13206106950";
                    HttpUtil.sendOkHttpRequest(url,new Callback() {
                        @Override
                        public void onFailure(Request request, IOException e) {
                            System.out.println(e);
                        }
                        @Override
                        public void onResponse(Response response) throws IOException {
                            String responseData = response.body().string();
                            text.setTextID(Integer.parseInt(responseData));
                            Looper.prepare();
                            Toast.makeText(EditTextActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }
                    });
                }
            }
        });

        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String textID=text.getTextID().toString();
                String url=texturl+"sendText"+"?textID="+textID;
                HttpUtil.sendOkHttpRequest(url,new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        System.out.println(e);
                    }
                    @Override
                    public void onResponse(Response response) throws IOException {
                        String responseData = response.body().string();
                        Toast.makeText(EditTextActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });


    }
    private String getJson(String textTitle,String textContent,String recvTele,String textID) {
        JSONObject jsonParam=new JSONObject();
        try {
            jsonParam.put("textID",textID);
            jsonParam.put("textTitle",textTitle);
            jsonParam.put("textContent",textContent);
            jsonParam.put("recvTele",recvTele);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonParam.toString();
    }

}